home *** CD-ROM | disk | FTP | other *** search
- #include <dos.h>
- #include <stdio.h>
- #define TRUE 1
- #define FALSE 0
- char curdir[64], newdir[64];
- int newdirlen, DONE;
- main(argc,argv)
- int argc;
- char *argv[];
- {
- char *fname, rootdir[64];
- int i;
-
- if (argc == 1) { /* If no arguments specified, */
- getcwd(curdir,64); /* then print the current */
- printf("%s\n",curdir); /* directory name and return */
- return;
- }
-
- if (argc > 2) { /* Invalid number of arguments */
- printf("Usage - CHD [pathname]\n");
- return;
- }
-
- strcpy(newdir,strupr(argv[1])); /* Make global copy of path name, */
- /* converting to uppercase. */
-
- if (chdir(newdir)) { /* Try make NEWDIR the new dir. */
- newdirlen = strlen(newdir); /* If fail, get name length. */
- if (newdir[0] != '\\') { /* Make a copy of the path name */
- rootdir[0] = '\\'; /* that has a backslash as the */
- strcpy(rootdir+1,newdir); /* first character. */
- }
- else strcpy(rootdir,newdir);
-
- if (chdir(rootdir)) { /* Try this name. */
- DONE = FALSE; /* If this fails, search down */
- searchdirs("\\"); /* the directory tree. */
- if (!DONE) printf("Invalid directory\n");
- }
- }
- }
-
-
- searchdirs(fname)
- char *fname;
- {
- struct FIND *pf;
- int i;
- char newname[64];
- char pfsave[sizeof(*pf)];
-
- i = index(fname,newdir[0]);
- while (i != NULL && strncmp(newdir,i,newdirlen) != 0) i = index(++i,newdir[0]);
- if (i != NULL) {
- DONE = TRUE;
- if (fname[strlen(fname)-1] == '\\') fname[strlen(fname)-1] = '\0';
- chdir(fname);
- return;
- }
-
- strcpy(newname,fname); /* the rightmost slash. */
- strcat(newname,"*."); /* */
-
- pf = findfirst(newname,0x10); /* Find the first subdir */
-
- /* Keep searching for more sudirs if the one we found wasn't a subdir, */
- /* or in case it was . or .. */
- while (pf != 0 && ((*pf).name[0] == '.' || (*pf).attribute != 0x10)) pf = findnext();
-
- for (i=0; i < sizeof(*pf); i++) pfsave[i] = (*pf).reserved[i];
-
- if (pf != 0) {
- strcpy(newname,fname); /* Build name of subdir to */
- strcat(newname,(*pf).name); /* find. */
- strcat(newname,"\\"); /* */
-
- searchdirs(newname); /* Apply routine recursively */
- if (DONE) return;
- while( pf != 0) {
- for (i=0; i < sizeof(*pf); i++) (*pf).reserved[i] = pfsave[i];
-
- if (pf != 0)
- do {
- pf = findnext();
- } while (pf != 0 && ((*pf).name[0] == '.' || (*pf).attribute != 0x10));
-
- if (pf != 0) {
- for (i=0; i < sizeof(*pf); i++) pfsave[i] = (*pf).reserved[i];
-
- strcpy(newname,fname);
- strcat(newname,(*pf).name);
- strcat(newname,"\\");
- searchdirs(newname);
- if (DONE) return;
- }
- }
- }
- }